package com.yygo.webapp.controller.loanrequest;

import com.yygo.model.Attachment;
import com.yygo.model.Dealer;
import com.yygo.model.LoanRequest;
import com.yygo.model.User;
import com.yygo.model.enums.Phase;
import com.yygo.model.enums.RequestStatus;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * Created by tiansha on 2015/7/10.
 */
@Controller
@RequestMapping("/copyRequest*")
public class CopyRequestController extends BaseLoanRequestFormController {

    @RequestMapping(method = RequestMethod.GET)
    protected ModelAndView showForm(HttpServletRequest request, Principal principal)
            throws Exception {
        String id = request.getParameter("reqId");
        LoanRequest loanRequest = null;
        if (!StringUtils.isBlank(id)) {
            loanRequest = loanRequestManager.loadWholeLoanRequest(new Long(id));
            loanRequest.setId(null);
          loanRequest.setCopyFromRequestId(new Long(id));

            if (loanRequest.getLoanProduct() != null) {
                loanRequest.setLoanProductId(loanRequest.getLoanProduct().getId());
            }
            loanRequest.getRequestPerson().setId(null);
            if (loanRequest.getCoPerson() != null) {
                loanRequest.setCoLoaned(true);
                loanRequest.getCoPerson().setId(null);
            }
            if (loanRequest.getDbPerson() != null) {
                loanRequest.setGuaranted(true);
                loanRequest.getDbPerson().setId(null);
            }

            loanRequest.setNumber(getRequestNumber());
            User loginUser = userManager.getUserByUsername(principal.getName());
            Dealer dealer = loginUser.getDealer();
            loanRequest.setDealer(dealer);
            loanRequest.setDealerUser(loginUser);
            loanRequest.setAuditUser(null);
            //loanRequest.setFeedbackType(FeedbackStatus.None);
            if (loanRequest.getPhase().equals(Phase.credit_denied)) {
                loanRequest.setStatus(RequestStatus.draft);
                loanRequest.setPhase(Phase.draft);
                loanRequest.setRequestPersonCreditQuestionnaire(null);
                loanRequest.setCoPersonCreditQuestionnaire(null);
                loanRequest.setDbPersonCreditQuestionnaire(null);

                // clean bank audit attachments
                List<Attachment> attachments = loanRequest.getAttachmentList();
                int i = 0;
                int len = attachments.size();

            } else {
                loanRequest.setStatus(RequestStatus.dealer_submit);
                loanRequest.setPhase(Phase.dealer_submit);
            }
            loanRequest.setRequestDate(new Date());
            loanRequest.setDecideDate(null);
            loanRequest.setLoanDate(null);

            loanRequest.getActivities().clear();
            //loanRequest.getTalkRecordList().clear();
            //loanRequest.getFeedbackList().clear();
            loanRequest.getRules().clear();

            //loanRequestManager.saveLoanRequest(loanRequest);
        } else {
            throw new IllegalAccessError("No request to copy.");
        }

        Model model = new ExtendedModelMap();

        model.addAttribute("loanRequest", loanRequest);
        model.addAttribute("copy", "true");
        String view = "loanRequestform";
        if (loanRequest.getPhase().equals(Phase.draft))
            view = "loanRequestInit";

        return new ModelAndView(view, model.asMap());
    }

}
